package com.monkey.controller;

import com.monkey.core.ErrorCode;
import com.monkey.core.WebRet;
import com.monkey.model.user.*;
import com.monkey.service.UserService;
import com.monkey.util.IDGenerator;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * desc:
 * auth: chenxiaobin
 * data: 2017/4/1.
 */
@RestController
@RequestMapping(value = "/user")
public class UserController {
    private static Logger logger = LoggerFactory.getLogger(UserController.class);

    @Resource
    private UserService userService = null;

    @Resource
    private IDGenerator idGenerator = null;

    /**
     * 校验手机号是否存在
     *
     * @param phone 手机号
     */
    @RequestMapping(value = "/check", method = RequestMethod.POST)
    public Map<String, Object> check_log(@RequestBody Map<String, Object> params) {
        Map<String, Object> ret = new HashMap<>();
        String phone = (String) params.get("phone");
        if (null == phone || phone.isEmpty()) {
            ret.put("code", ErrorCode.PARAM_ERROR);
            ret.put("msg", ErrorCode.getMsg(ErrorCode.PARAM_ERROR));
            return ret;
        }

        WebRet ret1 = userService.check(phone);
        ret.put("code", ret1.getCode());
        ret.put("msg", ret1.getMsg());
        return ret;
    }

    /**
     * 后台登录
     *
     * @param phone    手机号
     * @param password 密码
     * @return token 成功的话返回
     */
    @RequestMapping(value = "/login", method = RequestMethod.POST)
    public Map<String, Object> login_log(@RequestBody Map<String, Object> params) {
        Map<String, Object> ret = new HashMap<>();
        String phone = (String) params.get("phone");
        String password = (String) params.get("password");
        if (null == phone || phone.isEmpty() || null == password || password.isEmpty()) {
            ret.put("code", ErrorCode.PARAM_ERROR);
            ret.put("msg", ErrorCode.getMsg(ErrorCode.PARAM_ERROR));
            return ret;
        }

        UserLoginData data = userService.login(phone, password);
        if (ErrorCode.SUCCESS == data.getCode()) {
            ret.put("code", ErrorCode.SUCCESS);
            ret.put("msg", "");
            ret.put("token", data.getToken());
            ret.put("info", data.getData());
        } else {
            ret.put("code", data.getCode());
            ret.put("msg", data.getMsg());
        }
        return ret;
    }

    /**
     * 获取玩家信息
     *
     * @return info 玩家信息
     */
    @RequestMapping(value = "/getInfo", method = RequestMethod.POST)
    public Map<String, Object> getInfo_log(@RequestBody Map<String, Object> params, @RequestHeader String token) {
        Map<String, Object> ret = new HashMap<>();
        if (null == token || token.isEmpty()) {
            ret.put("code", ErrorCode.TOKEN_ERROR);
            ret.put("msg", ErrorCode.getMsg(ErrorCode.TOKEN_ERROR));
            return ret;
        }
        UserData user = userService.getInfo(token);
        if (null == user) {
            ret.put("code", ErrorCode.TOKEN_ERROR);
            ret.put("msg", ErrorCode.getMsg(ErrorCode.TOKEN_ERROR));
            return ret;
        }
        ret.put("code", ErrorCode.SUCCESS);
        ret.put("msg", "");
        ret.put("info", user);
        return ret;
    }

    /**
     * 获取验证码，注册/忘记密码
     *
     * @param phone string 手机号
     * @return value string 验证码(测试版本有返，正式版不返回)
     */
    @RequestMapping(value = "/getCode", method = RequestMethod.POST)
    public Map<String, Object> getCode_log(@RequestBody Map<String, Object> params) {
        Map<String, Object> ret = new HashMap<>();
        String phone = (String) params.get("phone");
        if (null == phone || phone.isEmpty()) {
            ret.put("code", ErrorCode.TOKEN_ERROR);
            ret.put("msg", ErrorCode.getMsg(ErrorCode.TOKEN_ERROR));
            return ret;
        }

        UserCode ret1 = userService.getCode(phone);
        ret.put("code", ret1.getCode());
        ret.put("msg", ret1.getMsg());
        return ret;
    }

    /**
     * 注册
     *
     * @param phone    string 手机号
     * @param password string 密码
     * @return token string 用于登录
     */
    @RequestMapping(value = "/register", method = RequestMethod.POST)
    public Map<String, Object> register_log(@RequestBody Map<String, Object> params) {
        Map<String, Object> ret = new HashMap<>();
        String phone = (String) params.get("phone");
        String password = (String) params.get("password");
        int code = (int) params.get("code");
        if (null == phone || phone.isEmpty() || null == password || password.isEmpty() || 0 == code) {
            ret.put("code", ErrorCode.PARAM_ERROR);
            ret.put("msg", ErrorCode.getMsg(ErrorCode.PARAM_ERROR));
            return ret;
        }

        UserRegisterData ret1 = userService.register(phone, password, code);
        if (ret1.isRet()) {
            String token = idGenerator.randomUUID();
            userService.insertToken(phone, ret1.getUserid(), token);
            ret.put("code", ErrorCode.SUCCESS);
            ret.put("msg", "");
            ret.put("token", token);
        } else {
            ret.put("code", ret1.getCode());
            ret.put("msg", ret1.getMsg());
        }
        return ret;
    }

    /**
     * 登出
     */
    @RequestMapping(value = "/logout", method = RequestMethod.POST)
    public Map<String, Object> logout_log(@RequestBody Map<String, Object> params, @RequestHeader String token) {
        Map<String, Object> ret = new HashMap<>();
        if (null == token || token.isEmpty()) {
            ret.put("code", ErrorCode.TOKEN_ERROR);
            ret.put("msg", ErrorCode.getMsg(ErrorCode.TOKEN_ERROR));
            return ret;
        }
        UserData user = userService.getInfo(token);
        if (null == user) {
            ret.put("code", ErrorCode.TOKEN_ERROR);
            ret.put("msg", ErrorCode.getMsg(ErrorCode.TOKEN_ERROR));
            return ret;
        }

        userService.deleteToken(token);
        ret.put("code", ErrorCode.SUCCESS);
        ret.put("msg", "");
        return ret;
    }

    /**
     * 修改密码
     *
     * @param phone string 手机号
     * @param pwd   string 密码
     * @param code  string 验证码
     */
    @RequestMapping(value = "/modifyPwdWithCode", method = RequestMethod.POST)
    public Map<String, Object> modifyPwdWithCode_log(@RequestBody Map<String, Object> params) {
        Map<String, Object> ret = new HashMap<>();
        String phone = (String) params.get("phone");
        String pwd = (String) params.get("pwd");
        String code = (String) params.get("code");
        if (null == phone || phone.isEmpty() || null == pwd || pwd.isEmpty() || null == code || code.isEmpty()) {
            ret.put("code", ErrorCode.PARAM_ERROR);
            ret.put("msg", ErrorCode.getMsg(ErrorCode.PARAM_ERROR));
            return ret;
        }

        WebRet ret1 = userService.modifyPwdWithCode(phone, pwd, Integer.valueOf(code));
        ret.put("code", ret1.getCode());
        ret.put("msg", ret1.getMsg());
        return ret;
    }

    /**
     * 修改密码(记得旧密码)
     *
     * @param oldPwd 旧密码
     * @param newPwd 新密码
     */
    @RequestMapping(value = "/modifyPwd", method = RequestMethod.POST)
    public Map<String, Object> modifyPwd_log(@RequestBody Map<String, Object> params, @RequestHeader String token) {
        Map<String, Object> ret = new HashMap<>();
        if (null == token || token.isEmpty()) {
            ret.put("code", ErrorCode.TOKEN_ERROR);
            ret.put("msg", ErrorCode.getMsg(ErrorCode.TOKEN_ERROR));
            return ret;
        }
        UserData user = userService.getInfo(token);
        if (null == user) {
            ret.put("code", ErrorCode.TOKEN_ERROR);
            ret.put("msg", ErrorCode.getMsg(ErrorCode.TOKEN_ERROR));
            return ret;
        }

        String oldPwd = (String) params.get("oldPwd");
        String newPwd = (String) params.get("newPwd");
        if (null == oldPwd || oldPwd.isEmpty() || null == newPwd || newPwd.isEmpty()) {
            ret.put("code", ErrorCode.PARAM_ERROR);
            ret.put("msg", ErrorCode.getMsg(ErrorCode.PARAM_ERROR));
            return ret;
        }

        WebRet ret1 = userService.modifyPwd(user.getPhone(), oldPwd, newPwd);
        ret.put("code", ret1.getCode());
        ret.put("msg", ret1.getMsg());
        return ret;
    }

    /**
     * 绑定钱包
     *
     * @param wallet string 钱包
     * @param code int 验证码
     */
    @RequestMapping(value = "/bindWallet", method = RequestMethod.POST)
    public Map<String, Object> bindWallet_log(@RequestBody Map<String, Object> params, @RequestHeader String token) {
        Map<String, Object> ret = new HashMap<>();
        if (null == token || token.isEmpty()) {
            ret.put("code", ErrorCode.TOKEN_ERROR);
            ret.put("msg", ErrorCode.getMsg(ErrorCode.TOKEN_ERROR));
            return ret;
        }
        User user = userService.getUserWithToken(token);
        if (null == user) {
            ret.put("code", ErrorCode.TOKEN_ERROR);
            ret.put("msg", ErrorCode.getMsg(ErrorCode.TOKEN_ERROR));
            return ret;
        }

        String wallet = (String) params.get("wallet");
        int code = (int) params.getOrDefault("code", 0);
        if (null == wallet || wallet.isEmpty()) {
            ret.put("code", ErrorCode.PARAM_ERROR);
            ret.put("msg", ErrorCode.getMsg(ErrorCode.PARAM_ERROR));
            return ret;
        }

        WebRet ret1 = userService.bindWallet(user, wallet, code);
        ret.put("code", ret1.getCode());
        ret.put("msg", ret1.getMsg());
        return ret;
    }

    /**
     * 修改用户名称
     *
     * @param name string 名字
     */
    @RequestMapping(value = "/modifyName", method = RequestMethod.POST)
    public Map<String, Object> modifyName_log(@RequestBody Map<String, Object> params, @RequestHeader String token) {
        Map<String, Object> ret = new HashMap<>();
        if (null == token || token.isEmpty()) {
            ret.put("code", ErrorCode.TOKEN_ERROR);
            ret.put("msg", ErrorCode.getMsg(ErrorCode.TOKEN_ERROR));
            return ret;
        }
        User user = userService.getUserWithToken(token);
        if (null == user) {
            ret.put("code", ErrorCode.TOKEN_ERROR);
            ret.put("msg", ErrorCode.getMsg(ErrorCode.TOKEN_ERROR));
            return ret;
        }

        String name = (String) params.get("name");
        if (null == name || name.isEmpty()) {
            ret.put("code", ErrorCode.PARAM_ERROR);
            ret.put("msg", ErrorCode.getMsg(ErrorCode.PARAM_ERROR));
            return ret;
        }

        WebRet ret1 = userService.modifyName(user, name);
        ret.put("code", ret1.getCode());
        ret.put("msg", ret1.getMsg());
        return ret;
    }

    /**
     * 获取用户收支记录
     *
     * @param offset int 偏移
     * @param num int 数量
     * @return list 收支记录，action: 喂食1，市场2，交配3，后台充值4，掘金5，充值6
     */
    @RequestMapping(value = "/getBillList", method = RequestMethod.POST)
    public Map<String, Object> getBillList(@RequestBody Map<String, Object> params, @RequestHeader String token) {
        Map<String, Object> ret = new HashMap<>();
        if (null == token || token.isEmpty()) {
            ret.put("code", ErrorCode.TOKEN_ERROR);
            ret.put("msg", ErrorCode.getMsg(ErrorCode.TOKEN_ERROR));
            return ret;
        }
        User user = userService.getUserWithToken(token);
        if (null == user) {
            ret.put("code", ErrorCode.TOKEN_ERROR);
            ret.put("msg", ErrorCode.getMsg(ErrorCode.TOKEN_ERROR));
            return ret;
        }
        int status = (int) params.getOrDefault("status", 0);
        int offset = (int) params.get("offset");
        int num = (int) params.get("num");
        if (0 > offset || 0 > num) {
            ret.put("code", ErrorCode.PARAM_ERROR);
            ret.put("msg", ErrorCode.getMsg(ErrorCode.PARAM_ERROR));
            return ret;
        }
        offset = (offset - 1) * num;
        List<UserBill> bills = userService.getBillList(user, status, offset, num);
        ret.put("code", ErrorCode.SUCCESS);
        ret.put("msg", ErrorCode.getMsg(ErrorCode.SUCCESS));
        ret.put("list", bills);
        return ret;
    }

    /**
     * 获取系统钱包
     *
     * @return wallet string 系统钱包
     */
    @RequestMapping(value = "/getSysWallet", method = RequestMethod.POST)
    public Map<String, Object> getSysWallet_log(@RequestBody Map<String, Object> params, @RequestHeader String token) {
        Map<String, Object> ret = new HashMap<>();
        if (null == token || token.isEmpty()) {
            ret.put("code", ErrorCode.TOKEN_ERROR);
            ret.put("msg", ErrorCode.getMsg(ErrorCode.TOKEN_ERROR));
            return ret;
        }
        UserData user = userService.getInfo(token);
        if (null == user) {
            ret.put("code", ErrorCode.TOKEN_ERROR);
            ret.put("msg", ErrorCode.getMsg(ErrorCode.TOKEN_ERROR));
            return ret;
        }

        ret.put("code", ErrorCode.SUCCESS);
        ret.put("msg", "");
        ret.put("wallet", userService.getSystemWallet());
        return ret;
    }
}
